package cn.changeforyou.web.dao.mybatis.ext.extBuilder.writer;

import cn.changeforyou.base.exception.ExceptionFactory;
import cn.changeforyou.web.dao.mybatis.ext.dao.BaseDao;
import cn.changeforyou.web.dao.mybatis.ext.exception.DaoExceptionEnum;
import cn.changeforyou.web.dao.mybatis.ext.extBuilder.SqlContext;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.FieldMetaInfo;
import cn.changeforyou.web.dao.mybatis.ext.metaInfo.MappedStatementParameterType;
import org.springframework.stereotype.Component;

/**
 * @author zhyu
 * @version 1.0
 * @date 2022/3/4 15:23
 */
@Component
public class WhereEmptyAndIdSqlWriter extends AbstractSqlWriter{

    @Override
    void doWriteSql(SqlContext context) {
        MappedStatementParameterType mappedStatementParameterType = context.getMappedStatementParameterType();
        switch (mappedStatementParameterType) {
            case selectEmpty:
                break;
            case deleteById:
            case selectById:
                SqlBuilder sql = context.getSql();
                sql.append(" WHERE ");
                FieldMetaInfo primaryKeyInfo = context.getPrimaryKeyInfo();
                sql.append(primaryKeyInfo.getColumnName()).append(" = ").append("#{").append(BaseDao.PRIMARY_KEY).append(
                        "}");
                break;
            default:
                throw ExceptionFactory.jsonException(DaoExceptionEnum.CODE_ERROR);
        }
    }

    @Override
    public SentenceEnum getSentenceEnum() {
        return SentenceEnum.where;
    }
}
